package com.yonyou.cyxdms.basedata.client.service.basedata;

import com.yonyou.cyx.framework.bean.dto.base.ClientInfoDTO;
import com.yonyou.cyx.framework.compent.redis.RedisClient;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyx.function.utils.common.CommonUtils;
import com.yonyou.cyx.function.utils.jsonserializer.JSONUtil;
import com.yonyou.cyxdms.basedata.client.constants.BaseDataCommonConstants;
import com.yonyou.cyxdms.basedata.client.domains.vo.PackageCacheVO;
import com.yonyou.cyxdms.basedata.client.domains.vo.VsOptionVO;
import com.yonyou.cyxdms.basedata.client.dto.option.VsOptionDTO;
import com.yonyou.cyxdms.basedata.client.feign.BaseDataCommonClient;
import com.yonyou.cyxdms.basedata.client.util.ConditionMapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2020/2/12.
 */
@Service
public class BaseCommonOptionServiceImpl implements BaseCommonOptionService {
    private final Logger logger = LoggerFactory.getLogger(BaseCommonPackageServiceImpl.class);

    @Autowired
    private BaseDataCommonClient baseDataCommonClient;

    @Autowired
    private RedisClient redisClient;

    /**
     * 根据选装id查询配置信息
     *
     */
    @Override
    public VsOptionVO getValueByOptionId(String optionId){
        ClientInfoDTO loginInfoDto = ApplicationContextHelper.getBeanByType(ClientInfoDTO.class);
        String messageFormat = MessageFormat.format(BaseDataCommonConstants.OPTION_CACHE_OPTIONID, loginInfoDto.getAppId());
        Object optionData = redisClient.hget(messageFormat, optionId);
        if (null == optionData) {
            // 重新加载这个id值的数据
            Map<String, Object> stringObjectMap = ConditionMapUtils.conditionMap(loginInfoDto.getAppId(), null);
            stringObjectMap.put("optionId", optionId);
            loadValueIfNotExists(stringObjectMap, messageFormat);
            Object redisOptionData = redisClient.hget(messageFormat, optionId);
            if (null == redisOptionData) {
                return null;
            }
            return JSONUtil.jsonToObj(JSONUtil.objectToJson(redisOptionData), VsOptionVO.class);
        } else {
            return JSONUtil.jsonToObj(JSONUtil.objectToJson(optionData), VsOptionVO.class);
        }
    }

    @Override
    public List<VsOptionVO> getValueByOptionAll(VsOptionDTO vsOptionDTO) {
        return baseDataCommonClient.queryVehicleProductOption(vsOptionDTO).getData();
    }

    @Override
    public List<VsOptionVO> getValueByOptionAllNewQuery(VsOptionDTO vsOptionDTO) {
        return baseDataCommonClient.queryVehicleProductOptionallNewQuery(vsOptionDTO).getData();
    }

    ;

    /**
     * 查询数据
     *
     * @param queryByMap
     * @author renwd
     * @since 2018/11/30
     */
    private void loadValueIfNotExists(Map<String, Object> queryByMap, String messageFormat) {
        List<VsOptionVO> optionDtoResponse = baseDataCommonClient.queryOptionByCondition(queryByMap).getData();
        logger.info("feign查询Package信息:{}", optionDtoResponse);

        if (!CommonUtils.isNullOrEmpty(optionDtoResponse)) {
            optionDtoResponse.stream().forEach(packageCacheDto -> initOneValue(packageCacheDto, messageFormat));
        }
    }

    /**
     * 数据放置redis
     *
     * @param optionCache packageCache
     * @author renwd
     * @since 2018/11/30
     */
    private void initOneValue(VsOptionVO optionCache, String key) {
        redisClient.hsetExpire(key, String.valueOf(optionCache.getPackageId()), JSONUtil.objectToJson(optionCache), BaseDataCommonConstants.EXPIRE_TIME_ONE_DAY);
    }
}
